package com.shujia.base.day03

/**
 *  Set集合元素，唯一且无序
 *
 *  上一个案例在List中操作的高阶函数，在Set中同样可以使用
 */
object Demo18Set {
  def main(args: Array[String]): Unit = {
    //创建一个Set集合
    val s1:Set[String] = Set("hello","world","java","hadoop","world","flink","hello","world")
    println(s"s1:${s1}")
    val s2:Set[String] = Set("spark","world","hive","hadoop","spark","hbase","hello","hive")
    println(s"s2:${s2}")

    println("="*50)
    /**
     * 交集，并集，差集
     */
    // 求两个Set集合的交集
//    val resSet1:Set[String] = s1.&(s2)
    val resSet1:Set[String] = s1 & s2
    println(s"s1:${s1}")
    println(s"s2:${s2}")
    println(s"resSet1:${resSet1}")
    println("="*50)
    //求两个Set集合的并集
//    val resSet2:Set[String] = s1.|(s2)
    val resSet2:Set[String] = s1 | s2
    println(s"s1:${s1}")
    println(s"s2:${s2}")
    println(s"resSet2:${resSet2}")
    println("="*50)
    //求两个Set集合的差集
//    val resSet3:Set[String] = s1.&~(s2)
    val resSet3:Set[String] = s1 &~ s2
    println(s"s1:${s1}")
    println(s"s2:${s2}")
    println(s"resSet3:${resSet3}")
    println("="*50)
    /**
     * 需求：对List集合中的元素进行去重
     * 1、List集合直接调用distinct函数进行去重
     * 2、List->Set->List
     */
    val list1:List[String] = List("hello","world","java","hadoop","world","flink","hello","world")
    //1、List集合直接调用distinct函数进行去重
//    val list2:List[String] = list1.distinct
//    println(s"list1:${list1}")
//    println(s"list2:${list2}")

    //2、List->Set->List
    val resSet4:Set[String] = list1.toSet
    println(s"list1:${list1}")
    println(s"resSet4:${resSet4}")
    //Set->List
    val list2:List[String] = resSet4.toList
    println(s"list2:${list2}")

  }
}
